package com.example.flshtarvel.controller;

import com.example.flshtarvel.entity.Collect;
import com.example.flshtarvel.entity.Like;
import com.example.flshtarvel.service.LikeService;
import com.example.flshtarvel.service.RouteLikeMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class LikeController {
//    @Autowired
//    RouteLikeMapService routeLikeMapService;
    @Autowired
    LikeService likeService;

    //点赞
    @PostMapping("/like")
    public Map<String,Object> like(Like like, HttpSession session){
//    @GetMapping("/routeLike")
//    public Map<String,Object> routeLike(@RequestParam("routeId")String routeId,
//                                        @RequestParam("userId")String userId){
        Map<String,Object> map = new HashMap<String,Object>();
        like.setUserId((String)session.getAttribute("userId"));
        int result = likeService.like(like);
        if(result==1){
            map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        return map;
    }
    //取消点赞
    @GetMapping("/deleteLike")
    public Map<String,Object> deleteLike(@RequestParam("id")String messageId, HttpSession session){
//    @GetMapping("/deleteRouteLike")
//    public Map<String,Object> deleteRouteLike(@RequestParam("routeId")String routeId,
//                                                @RequestParam("userId")String userId){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = likeService.deleteLike(messageId,(String)session.getAttribute("userId"));
        if(result >= 1){
            map.put("msg",1);
        }else{
            map.put("msg",0);
        }
        return map;
    }
    //查询点赞    根据路线id 用户id
    @GetMapping("/seleteLike")
    public Map<String,Object> selectLikeByMessageIdAndUserId(@RequestParam("messageId")String messageId,
                                                                HttpSession session){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = likeService.selectLikeByMessageIdAndUserId(messageId,(String)session.getAttribute("userId"));
        if(result==1){
            System.out.println("1");
        }else{
            System.out.println("0");
        }
        return map;
    }

    //查询点赞数   根据路线id
    @GetMapping("/seleteLikeCount")
    public Map<String,Object> selectLikeByMessageId(String routeId){
        Map<String,Object> map = new HashMap<String,Object>();
        int result = likeService.selectLikeByMessageId(routeId);
        map.put("msg",1);
        map.put("count",result);
        return map;
    }

    //查询点赞 根据用户id
    @GetMapping("/selectLikeByUserId")
    public Map<String,Object> selectLikeByUserId(HttpSession session){
        Map<String,Object> map = new HashMap<>();
        List<Like> likeList = likeService.selectLikeByUserId((String)session.getAttribute("userId"));
        if(likeList.size()<=0){
            map.put("msg",0);
        }else{
            map.put("msg",1);
            map.put("resultList",likeList);
        }
        return map;
    }


}
